/*
 * Description: 跨页面通信
 * Author: wangyifei
 * DateCreated: 2024-02-28 09:28
 */

import { useEffect } from 'react';
import { Button, Space } from 'antd';
import { openWindowFeature } from '@/utils/openWindowFeature';
import { createCrossTagMsg } from '@/utils/crossTagMsg';
import Test from './Test';

export const [sendMsg, listenMsg] = createCrossTagMsg<{ name: string }>('wyf');

const BroadcastChannel = () => {
	useEffect(() => {
		const close = listenMsg((data) => {
			console.log('监听到跨页面数据：', data);
		});

		return () => close();
	}, []);

	return (
		<Space>
			<Button
				onClick={() => {
					openWindowFeature('/target-blank?name=frank&age=18');
					window['_filter'] = {
						name: 'frank',
						age: 18,
						height: 1.88
					};
					console.log(window);
				}}
			>
				打开新窗口
			</Button>
			<Button onClick={() => sendMsg({ name: 'wyf' })}>send</Button>
			<Test />
		</Space>
	);
};

export default BroadcastChannel;
